make_figure <- function(.data, .variable, .title, .study) {
  out1 <- .data %>%
    group_by(!!{{ .variable }}, pair) %>%
    reframe(tidy(lm_robust(selected ~ 1, data = pick(everything())))) %>%
    mutate(y = case_when(
      {{ .variable }} == "Low" ~ pair - 0.2,
      {{ .variable }} == "Medium" ~ pair,
      {{ .variable }} == "High" ~ pair + 0.2,
      {{ .variable }} == "Female" ~ pair + 0.1,
      {{ .variable }} == "Male" ~ pair - 0.1,
      {{ .variable }} == "Left" ~ pair + 0.2,
      {{ .variable }} == "Middle of the Road" ~ pair,
      {{ .variable }} == "Right" ~ pair - 0.2,
      {{ .variable }} == "College/University Graduate" ~ pair + 0.15,
      {{ .variable }} == "Not a College/University Graduate" ~ pair - 0.15,
      {{ .variable }} == TRUE ~ pair + 0.1,
      {{ .variable }} == FALSE ~ pair - 0.1,
      {{ .variable }} == "Osaka" ~ pair + 0.15,
      {{ .variable }} == "Hyogo" ~ pair,
      {{ .variable }} == "Kyoto" ~ pair - 0.15,
      {{ .variable }} == "18 to 43" ~ pair + 0.15,
      {{ .variable }} == "44 to 65" ~ pair,
      {{ .variable }} == "66+" ~ pair - 0.15,
      {{ .variable }} == "Male vs. Male" ~ pair + 0.3,
      {{ .variable }} == "Female vs. Female" ~ pair + 0.1,
      {{ .variable }} == "Male vs. Female" ~ pair - 0.1,
      {{ .variable }} == "Female vs. Male" ~ pair - 0.3
    ))

  out2 <- .data %>%
    group_by(!!{{ .variable }}) %>%
    reframe(tidy(lm_robust(selected ~ pair, data = pick(everything())))) %>%
    filter(term != "(Intercept)") %>%
    mutate(y = case_when(
      {{ .variable }} == "Low" ~ 1 - 0.2,
      {{ .variable }} == "Medium" ~ 1,
      {{ .variable }} == "High" ~ 1 + 0.2,
      {{ .variable }} == "Female" ~ 1 + 0.1,
      {{ .variable }} == "Male" ~ 1 - 0.1,
      {{ .variable }} == "Left" ~ 1 + 0.2,
      {{ .variable }} == "Middle of the Road" ~ 1,
      {{ .variable }} == "Right" ~ 1 - 0.2,
      {{ .variable }} == "College/University Graduate" ~ 1 + 0.15,
      {{ .variable }} == "Not a College/University Graduate" ~ 1 - 0.15,
      {{ .variable }} == TRUE ~ 1 + 0.1,
      {{ .variable }} == FALSE ~ 1 - 0.1,
      {{ .variable }} == "Osaka" ~ 1 + 0.15,
      {{ .variable }} == "Hyogo" ~ 1,
      {{ .variable }} == "Kyoto" ~ 1 - 0.15,
      {{ .variable }} == "18 to 43" ~ 1 + 0.15,
      {{ .variable }} == "44 to 65" ~ 1,
      {{ .variable }} == "66+" ~ 1 - 0.15,
      {{ .variable }} == "Male vs. Male" ~ 1 + 0.3,
      {{ .variable }} == "Female vs. Female" ~ 1 + 0.1,
      {{ .variable }} == "Male vs. Female" ~ 1 - 0.1,
      {{ .variable }} == "Female vs. Male" ~ 1 - 0.3
    ))

  var_name <- rlang::as_name(rlang::ensym(.variable))

  g1 <- ggplot(out1) +
    geom_vline(
      xintercept = 0.5,
      linetype = "dashed",
      color = "gray"
    ) +
    geom_text(aes(
      y = y,
      x = estimate,
      label = format(round(estimate, digits = 2), nsmall = 2)
    ), vjust = -0.5) +
    geom_pointrange(aes(
      y = y,
      x = estimate,
      xmin = conf.low,
      xmax = conf.high,
      color = {{ .variable }}
    )) +
    scale_y_continuous(
      limits = c(0.5, 2.5),
      breaks = c(1, 2),
      labels = c(
        "Japanese vs.\nZainichi (in Japanese)",
        "Japanese vs.\nZainichi (in Korean)"
      )
    ) +
    scale_color_colorblind() +
    theme_few() +
    theme(legend.position = "top") +
    labs(
      title = str_c("Marginal Mean by ", .title),
      y = NULL,
      color = .title
    )

  g1

  g2 <- ggplot(out2) +
    geom_vline(
      xintercept = 0,
      linetype = "dashed",
      color = "gray"
    ) +
    geom_text(aes(
      y = y,
      x = estimate,
      label = format(round(estimate, digits = 2), nsmall = 2)
    ), vjust = -0.5) +
    geom_pointrange(aes(
      y = y,
      x = estimate,
      xmin = conf.low,
      xmax = conf.high,
      color = {{ .variable }}
    )) +
    scale_color_colorblind() +
    scale_y_continuous(
      limits = c(0.5, 1.5),
      breaks = c(1),
      labels = c("Difference in\n Marginal Means")
    ) +
    theme_few() +
    theme(legend.position = "top") +
    labs(
      title = str_c("Average Marginal Component Effect by ", .title),
      color = .title,
      y = NULL
    )

  g2

  g1 / g2 +
    plot_layout(
      heights = c(2, 1),
      guides = "collect"
    ) &
    theme(legend.position = "top")
}
